package service;

import dao.OrderDao;
import entity.Order;

import java.util.List;
import java.util.Map;

public class OrderService {
    private OrderDao orderDao = new OrderDao();

    /**
     * 添加订单
     */
    public boolean addOrder(String userName, String productName, String type, Float price){
        Order order = new Order(null, userName, productName, type, price, 0);
        System.out.println(order);
        int row = orderDao.add(order);
        return row == 1;
    }

    /**
     * 通过用户名查询订单(多表查询)
     */
    public List<Map<String, Object>> queryOrderByUserName(String userName){
        String sql = "SELECT o.*, a.picture_url FROM `order` AS o, attraction as a WHERE user_name=? " +
                "AND `type`='旅游景点' AND o.product_name=a.`name`";
        List<Map<String, Object>> res = orderDao.query(sql, userName);
        String sql2 = "SELECT o.*, r.picture_url FROM `order` AS o, `route` as r WHERE user_name=? " +
                "AND `type`='旅游路线' AND o.product_name=r.`title`";
        res.addAll(orderDao.query(sql2, userName));
        return res;
    }

    /**
     * 支付
     */
    public boolean pay(int id){
        String sql = "update `order` set status=1 where id=?";
        int cnt = orderDao.update(sql, id);
        return cnt == 1;
    }

    /**
     * 取消订单
     */
    public boolean revocation(int id){
        String sql = "update `order` set status=2 where id=?";
        int cnt = orderDao.update(sql, id);
        return cnt == 1;
    }
}
